﻿Imports SistemaINCA.Security

Namespace Models

    Public Interface IAlumnos
        <Authorize(Roles:="ERA")> _
        Function BuscarAlumnoByNombreOApellido(parametro As String) As List(Of FIND_ALUMNOS_BY_LETTERS_DATATYPE)

        <Authorize(Roles:="ERA")> _
        Function GetAlumno(NIE As String) As ALUMNO_DATATYPE

        <Authorize(Roles:="ERA")> _
        Sub UpdateDatosPersonales(NIE As String, DUI As String, Direccion As String, Departamento As Integer, Municipio As Integer, Telefono As String, Celular As String, CorreoE As String, MFamilia As Integer, Trabajando As Boolean, THijos As Boolean, EPadece As String, MPermanente As String)

        <Authorize(Roles:="ERA")> _
        Function DesactivarAlumno(NIE As String) As Boolean

        <Authorize(Roles:="ERA")> _
        Function GetNotas_ByAñoEscolar(NIE As String, AñoEscolar As String) As List(Of GET_NOTASALUMNO_BY_ANIOESCOLAR_DATATYPE)

        <Authorize(Roles:="ERA")> _
        Function GetNotas_ByAñoEscolar_PIVOT(NIE As String, AñoEscolar As String) As List(Of ViewModels.MateriaContentNotasClass)
    End Interface

    Partial Public Class SchoolClickDB_Repositorio
        Implements IAlumnos

        Public ReadOnly Property IAlumnos() As IAlumnos
            Get
                Return DirectCast(Me, IAlumnos)
            End Get
        End Property

        Private Function BuscarAlumnoByNombreOApellido(parametro As String) As List(Of FIND_ALUMNOS_BY_LETTERS_DATATYPE) Implements IAlumnos.BuscarAlumnoByNombreOApellido
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.FIND_ALUMNOS_BY_LETTERS(parametro).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido realizar la busqueda de alumnos en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function GetAlumno(NIE As String) As ALUMNO_DATATYPE Implements IAlumnos.GetAlumno
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.GET_ALUMNO_BYCOD(NIE).SingleOrDefault
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido recuperar los datos del alumno con No. de NIE: {1}. {0}", ex.Message, NIE), ex)
            End Try
        End Function

        Private Sub UpdateDatosPersonales(NIE As String, DUI As String, Direccion As String, Departamento As Integer, Municipio As Integer, Telefono As String, Celular As String, CorreoE As String, MFamilia As Integer, Trabajando As Boolean, THijos As Boolean, EPadece As String, MPermanente As String) Implements IAlumnos.UpdateDatosPersonales
            Try
                Using cnn As New SchoolClickEntities
                    cnn.UPT_DATOS_ALUMNO(NIE, DUI, Direccion, Departamento, Municipio, Telefono, Celular, CorreoE, MFamilia, Trabajando, THijos, EPadece, MPermanente)
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se pudieron guardar los nuevos cambios en los datos personales del alumno con No. de NIE: {1}. {0}", ex.Message, NIE), ex)
            End Try
        End Sub

        Private Function DesactivarAlumno(NIE As String) As Boolean Implements IAlumnos.DesactivarAlumno
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.OP_DESACTIVAR_ALUMNO(NIE, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("Rol_ERA")))).SingleOrDefault
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido desactivar el alumno con No. de NIE: {1}. {0}", ex.Message, NIE), ex)
            End Try
        End Function

        Private Function GetNotas_ByAñoEscolar(NIE As String, AñoEscolar As String) As List(Of GET_NOTASALUMNO_BY_ANIOESCOLAR_DATATYPE) Implements IAlumnos.GetNotas_ByAñoEscolar
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.GET_NOTASALUMNO_BY_ANIOESCOLAR(NIE, AñoEscolar, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("Rol_ERA")))).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido obtener las notas del alumno con No. de NIE: {1} en el Año Escolar {2}. {0}", ex.Message, NIE, AñoEscolar), ex)
            End Try
        End Function

        Private Function GetNotas_ByAñoEscolar_PIVOT(NIE As String, AñoEscolar As String) As List(Of ViewModels.MateriaContentNotasClass) Implements IAlumnos.GetNotas_ByAñoEscolar_PIVOT
            Try
                Using cnn As New SchoolClickEntities
                    Dim _sPS = "PS"  'Variable para omitir en el promedio y en el listado de periodos el Examen de Sufi
                    Dim q = (From r In cnn.GET_NOTASALUMNO_BY_ANIOESCOLAR(NIE, AñoEscolar, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("Rol_ERA"))))
                             Group By r.ID_MATERIA, r.MATERIA Into g = Group
                             Order By MATERIA
                             Select New Models.ViewModels.MateriaContentNotasClass With {
                                    .ID_Materia = ID_MATERIA, _
                                    .Materia = MATERIA, _
                                    .Promedio = Math.Round(g.Where(Function(m) m.PERIODO <> _sPS) _
                                                            .Average(Function(m) m.NOTA_NUM), 0).ToString("#0.0"), _
                                    .ExaSufi = g.Where(Function(m) m.PERIODO = _sPS And m.ID_MATERIA = ID_MATERIA) _
                                                .Select(Function(m) m.NOTA_DISP).SingleOrDefault, _
                                    .Periodos = (From pn In g
                                                 Where pn.PERIODO <> _sPS
                                                 Order By pn.PERIODO
                                                 Select
                                                        Periodo = pn.PERIODO,
                                                        Nota = pn.NOTA_DISP).ToList}).ToList
                    Return q
                End Using
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        'Public Function GET_INFO_PERSONAL_ALUMNO(Carnet As String) As GET_INFO_PERSONAL_BYALUMNO_DATATYPE
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Return cnn.GET_INFO_PERSONAL_BYALUMNO(Carnet).Single()
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar la informacion personal del alumno", ex)
        '    End Try
        'End Function

        'Public Function GET_NOTAS_RESUMEN_LAST_PERIODO_LAST_YEAR(Carnet As String) As List(Of GET_RESUMEN_NOTAS_PERIODO_ACTIVO_BYALUMNO_DATATYPE)
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Dim varNotas = cnn.GET_RESUMEN_NOTAS_PERIODO_ACTIVO_BYALUMNO(Carnet).ToList
        '            Dim varPorcentaje = From p In cnn.ACTIVIDADES_X_DEFAULT
        '                                Select p


        '            varNotas.Add(New GET_RESUMEN_NOTAS_PERIODO_ACTIVO_BYALUMNO_DATATYPE() With {
        '                                .ID_MATERIA = 0,
        '                                .MATERIA = "PT",
        '                                .A1 = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "A1" Select a1.PORCENT).Single,
        '                                .A2 = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "A2" Select a1.PORCENT).Single,
        '                                .AE = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "AE" Select a1.PORCENT).Single,
        '                                .HE = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "HE" Select a1.PORCENT).Single,
        '                                .PO = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "PO" Select a1.PORCENT).Single
        '                            })
        '            Return varNotas
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar el resumen de notas del alumno", ex)
        '    End Try
        'End Function

        'Public Function GET_NOTAS_BY_AÑO_AND_PERIODO(Alumno As String, Periodo As String, Año As String) As List(Of GET_RESUMEN_NOTAS_PERIODO_ACTIVO_BYALUMNO_DATATYPE)
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Dim varNotas = cnn.GET_NOTAS_ALUMNO_BY_PERIODO_Y_ANIO(Año, Periodo, Alumno).ToList
        '            Dim varPorcentaje = From p In cnn.ACTIVIDADES_X_DEFAULT
        '                                Select p


        '            varNotas.Add(New GET_RESUMEN_NOTAS_PERIODO_ACTIVO_BYALUMNO_DATATYPE() With {
        '                                .ID_MATERIA = 0,
        '                                .MATERIA = "PT",
        '                                .A1 = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "A1" Select a1.PORCENT).Single,
        '                                .A2 = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "A2" Select a1.PORCENT).Single,
        '                                .AE = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "AE" Select a1.PORCENT).Single,
        '                                .HE = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "HE" Select a1.PORCENT).Single,
        '                                .PO = (From a1 In varPorcentaje Where a1.ID_ACTDEFAULT = "PO" Select a1.PORCENT).Single
        '                            })
        '            Return varNotas
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar el resumen de notas del alumno", ex)
        '    End Try
        'End Function

        'Public Function Get_AllPeriodos_Por_Alumno(carnet As String) As List(Of GET_ALL_PERIODOS_BY_ALUMNO_DATATYPE)
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Return cnn.GET_ALL_PERIODOS_BY_ALUMNO(carnet).ToList
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar los periodos de evaluacion del alumno", ex)
        '    End Try
        'End Function

        'Public Function Get_Año_y_Periodo_by_IDPeriodo(Alumno As String, IDPeriodo As Integer) As GET_ANIO_AND_PERIODO_BY_IDPERIODO_DE_ALUMNO_DATATYPE
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Return cnn.GET_ANIO_AND_PERIODO_BY_IDPERIODO_DE_ALUMNO(IDPeriodo, Alumno).Single
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar las notas del periodo especificado", ex)
        '    End Try
        'End Function

        'Public Function Get_Detalle_Notas_ByMateria(Alumno As String, Periodo As String, Año As String, Materia As Integer) As List(Of GET_DETALLE_ACT_NOTAS_BYPERIODO_BYMATERIA_DATATYPE)
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Return cnn.GET_DETALLE_ACT_NOTAS_BYPERIODO_BYMATERIA(Alumno, Periodo, Año, Materia).ToList
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar el detalle de las notas de la materia", ex)
        '    End Try
        'End Function

        'Public Function Get_Max_and_Min_AñosGrados(Alumno As String) As GET_MAX_AND_MIN_YEAR_BY_ALUMNO_DATATYPE
        '    Try
        '        Using cnn As New SchoolClickEntities
        '            Return cnn.GET_MAX_AND_MIN_YEAR_BY_ALUMNO(Alumno).Single
        '        End Using
        '    Catch ex As Exception
        '        Throw New ApplicationException("No se pudo recuperar los años maximos y minimos del alumno", ex)
        '    End Try
        'End Function
    End Class
End Namespace
